package com.edmendst.xmall.controller;

import com.edmendst.xmall.dto.OrderDetail;
import com.edmendst.xmall.dto.OrderInfo;
import com.edmendst.xmall.dto.OrderItem;
import com.edmendst.xmall.dto.OrdersStatus;
import com.edmendst.xmall.service.OrderService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.ibatis.annotations.SelectKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author mjz
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
//    // 添加订单
//    OrderItem addOrder(OrderInfo orderInfo);
//
    @PostMapping
    public OrderItem addOrder(@RequestBody OrderDetail orderDetail) throws JsonProcessingException {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        OrderInfo orderInfo = new OrderInfo(username, orderDetail.getProductId(), orderDetail.getQuantity(), orderDetail.getDeliveryInfo());
        System.out.println(orderInfo);
        return orderService.addOrder(orderInfo);
    }
//    // 获取订单详情
//    OrderItem getOrderDetail(String orderId);
   @GetMapping("/{orderId}")
    public OrderItem getOrderDetail(@PathVariable String orderId){
        return orderService.getOrderDetail(orderId);
    }

//
//
//
//    int deleteOrder(String orderId, String userName);
    @DeleteMapping("/{orderId}")
    public int deleteOrder(@PathVariable String orderId){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        return orderService.deleteOrder(orderId, username);
    }

    @GetMapping
    public List<OrderItem> getOrderList(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        return orderService.getOrderList(username);
    }

//    // 签收订单
//    int updateOrderReceivedStatus(String orderId, Integer orderReceivedStatus);
    @PutMapping("/received")
    public int updateOrderReceivedStatus(@RequestBody OrdersStatus ordersStatus){
        return orderService.updateOrderReceivedStatus(ordersStatus.getOrderId(),  ordersStatus.getOrderReceivedStatus());
    }
//
//    //获取商店中的订单
//    List<OrderItem> getOrderListByShop(Integer shopId);
    @GetMapping("/shop/{shopId}")
    public List<OrderItem> getOrderListByShop(@PathVariable Integer shopId){
        return orderService.getOrderListByShop(shopId);
    }
//
//    //订单是否发货
//    int updateOrderDeliveryStatus(String orderId, Integer orderDeliveryStatus);
    @PutMapping("/delivery")
    public int updateOrderDeliveryStatus(@RequestBody OrdersStatus ordersStatus){
        return orderService.updateOrderDeliveryStatus(ordersStatus.getOrderId(), ordersStatus.getOrderDeliveryStatus());
    }

}
